package com.exam.demo.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.exam.demo.common.Result;
import com.exam.demo.entity.Role;
import com.exam.demo.service.RoleService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * 功能描述
 *
 * @author yxj30
 * @date 2022/03/16  19:18
 */
@RestController
@RequestMapping("/role")
public class RoleController {
    @Resource
    RoleService roleService;

    /**
     * 绑定角色和菜单的关系
     * @param roleId 角色id
     * @param menuIds 菜单id数组
     * @return
     */
    @PostMapping("/roleMenu/{roleId}")
    public Result roleMenu(@PathVariable Integer roleId, @RequestBody List<Integer> menuIds) {
        roleService.setRoleMenu(roleId, menuIds);
        return Result.success();
    }
    //获取绑定角色和菜单的关系
    @GetMapping("/roleMenu/{roleId}")
    public Result getRoleMenu(@PathVariable Integer roleId) {
        return Result.success( roleService.getRoleMenu(roleId));
    }


    //查找页面数据
    @GetMapping
    public Result findPage(@RequestParam Integer pageNum,
                           @RequestParam Integer pageSize,
                           @RequestParam(defaultValue = "") String roleName){
        QueryWrapper<Role> queryWrapper = new QueryWrapper<>();
        queryWrapper.like("name",roleName);
        Page<Role> page = roleService.page(new Page<>(pageNum, pageSize), queryWrapper);
        return Result.success(page);
    }

    //更改和插入
    @PostMapping("/save")
    public Result insert(@RequestBody Role role){
        return Result.success(roleService.saveOrUpdate(role));
    }

    //删除
    @DeleteMapping("/delete/{id}")
    public Result delete(@PathVariable Integer id){
        return Result.success(roleService.removeById(id));
    }

    //查找根据id全部专业数据
    @GetMapping("/selectByCollegeId/{college_id}")
    public Result findPage(@PathVariable String college_id){
        QueryWrapper<Role> queryWrapper = new QueryWrapper<>();
        List<Role> list = roleService.list(queryWrapper.eq("college_id",college_id));
        return Result.success(list);
    }

}
